/*
 * Created by JFormDesigner on Sun Jun 18 12:25:34 CST 2023
 */

package fm.warehousing;

import javax.swing.border.*;

import dao.*;
import fm.supplier.AddSupplier;
import fm.trade.TradeAdd;
import main.MainFm;
import model.Supplier;
import model.Trade;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

/**
 * @author YangC
 */
public class InStorage extends JInternalFrame {
    DefaultTableModel model;
    Vector<Vector<String> > results = new Vector<>();


    Vector<String> columnNames = new Vector<>();

    public InStorage() {
        initComponents();
        iniData();
        iniData_1();

        columnNames.add("编号");
        columnNames.add("名称");
        columnNames.add("类别");
        columnNames.add("单价");
        columnNames.add("数量");
        columnNames.add("码洋");
        columnNames.add("折扣");
        columnNames.add("实洋");

        model = new DefaultTableModel(results,columnNames);
        table1.setModel(model);
    }

    private void addTrade(ActionEvent e) {
        TradeAdd tradeAdd = new TradeAdd();
        tradeAdd.setBounds(0,0,490,365);
        MainFm.getDesktopPane().add(tradeAdd,JLayeredPane.MODAL_LAYER);

    }

    private void addSupplier(ActionEvent e) {
        AddSupplier addSupplier = new AddSupplier();
        addSupplier.setBounds(0,0,575,410);
        MainFm.getDesktopPane().add(addSupplier,JLayeredPane.MODAL_LAYER);
    }

    public void iniData() {
        Vector<String> its = Dao_supplier.getSupplier();
        DefaultComboBoxModel<String> model = new DefaultComboBoxModel<>(its);
        supplierComboBox.setModel(model);

        Vector<String> its_1 = Dao_trade.getTrade();
        DefaultComboBoxModel<String> model_1 = new DefaultComboBoxModel<>(its_1);
        tradeComboBox.setModel(model_1);

}

    public void iniData_1()
    {
        String supplierName = (String) supplierComboBox.getSelectedItem();

        List list = Dao_supplier.selectAll_name(supplierName);
        List<String> results = new ArrayList<>();

        for (Object i : list) {
            Supplier supplier = (Supplier) i;

            results.add(supplier.getSupplierID());
            results.add(supplier.getSupplierPeople());
            results.add(supplier.getAddress());
            results.add(supplier.getSupplierPhone());
        }

        supplierIDTextField.setText(results.get(0));
        manTextField.setText(results.get(1));
        phoneTextField.setText(results.get(3));
        addressTextField.setText(results.get(2));

        String tradeName = (String) tradeComboBox.getSelectedItem();


        List lists = Dao_trade.fuzzySelect_2(tradeName);
        String result = null;

        for (Object i : lists) {
            Trade trade = (Trade) i;

            result = String.valueOf(trade.getPrice());
        }

        priceTextField.setText(result);

    }

    private void cancel(ActionEvent e) {
        doDefaultCloseAction();
    }

    private void ok(ActionEvent e) {
        String tradeName = (String) tradeComboBox.getSelectedItem();

        if(Integer.parseInt(numberTextField.getText()) + Dao_trade.getTradeName_number(String.valueOf(tradeComboBox.getSelectedItem())) > Dao_type.getTypeID_max(Dao_trade.getTradeName_typeID(String.valueOf(tradeComboBox.getSelectedItem()))))
        {
            JOptionPane.showMessageDialog(null,"库存溢出！");
            return;
        }

        List lists = Dao_trade.fuzzySelect_2(tradeName);
        String result = null;

        for (Object i : lists) {
            Trade trade = (Trade) i;

            result = String.valueOf(trade.getDiscount());
        }

        int number = Integer.parseInt(numberTextField.getText());
        double price = Dao_trade.getTradeName_price(String.valueOf(tradeComboBox.getSelectedItem()));
        double reals = price * number;
        priceTextField.setText(String.valueOf(price));
        realsTextField.setText(String.valueOf( String.format("%.2f", reals * Double.parseDouble(result))));
        totalTextField.setText(String.valueOf(number * price));
        discountTextField.setText(String.valueOf(String.format("%.2f", number * price * (1 - Double.parseDouble(result)))));
    }

    private void add(ActionEvent e) {
        Vector<String> result = new Vector<String>();

        result.add(Dao_trade.getTradeName_tradeID(String.valueOf(tradeComboBox.getSelectedItem())));
        result.add(String.valueOf(tradeComboBox.getSelectedItem()));
        result.add(Dao_type.getTypeID_type(Dao_trade.getTradeName_typeID(String.valueOf(tradeComboBox.getSelectedItem()))));
        result.add(priceTextField.getText());
        result.add(numberTextField.getText());
        result.add(totalTextField.getText());
        result.add(String.valueOf(Dao_trade.getTradeName_discount(String.valueOf(tradeComboBox.getSelectedItem()))));

        result.add(realsTextField.getText());

        results.add(result);
        model.setDataVector(results,columnNames);

        priceTextField.setText("");

    }

    private void yes(ActionEvent e) {
        int setRow = table1.getRowCount();

        double totals = 0.0;
        double reals = 0.0;
        for (int i = 0; i < setRow; i++)
        {
            double total = Double.parseDouble(table1.getValueAt(i, 5).toString().trim());
            double real = Double.parseDouble(table1.getValueAt(i, 7).toString().trim());

            totals += total;
            reals += real;
        }

        timeTextField10.setText(Dao_No.getTime());

        totalsTextField8.setText(String.valueOf(totals));
        realTsTextField.setText(String.valueOf(reals));
    }

    private void enter(ActionEvent e) {
        String i_no = Dao_No.getNo_oder();
        String s_no = String.valueOf(supplierIDTextField.getText());
        String i_discount = "0";
        String i_total = "0";
        String i_real = "0";
        int i = Dao_warehousing.addIn(i_no, s_no, i_discount, i_total, i_real);

        int setRow = table1.getRowCount();
        String i_time = timeTextField10.getText();

        int j = 0;
        for (int t = 0; t < setRow; t++) {
            String id_no = Dao_No.getNo_in();
            String c_no = table1.getValueAt(t, 0).toString().trim();
            String i_number = table1.getValueAt(t, 4).toString().trim();
            String i_price = table1.getValueAt(t, 5).toString().trim();
            String i_realPrice = table1.getValueAt(t, 7).toString().trim();

            j = Dao_warehousing.add(id_no, c_no, i_no, i_number, i_price, i_realPrice, i_time);
        }

        if (i == i && j == 1)
        {
            JOptionPane.showMessageDialog(null,"入库成功！");
        }

    }

    private void refresh(ActionEvent e) {
        iniData();
    }

    private void supplier(ActionEvent e) {
        iniData_1();
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        panel1 = new JPanel();
        enterButton = new JButton();
        cancelButton = new JButton();
        panel2 = new JPanel();
        label1 = new JLabel();
        supplierComboBox = new JComboBox();
        supplierIDTextField = new JTextField();
        label2 = new JLabel();
        label3 = new JLabel();
        manTextField = new JTextField();
        label4 = new JLabel();
        addressTextField = new JTextField();
        label5 = new JLabel();
        phoneTextField = new JTextField();
        addSupplierButton = new JButton();
        refreshButton = new JButton();
        panel3 = new JPanel();
        panel4 = new JPanel();
        addButton = new JButton();
        yesButton = new JButton();
        panel5 = new JPanel();
        label6 = new JLabel();
        tradeComboBox = new JComboBox();
        label7 = new JLabel();
        numberTextField = new JTextField();
        addTradeButton = new JButton();
        label8 = new JLabel();
        priceTextField = new JTextField();
        label9 = new JLabel();
        totalTextField = new JTextField();
        label13 = new JLabel();
        discountTextField = new JTextField();
        label14 = new JLabel();
        realsTextField = new JTextField();
        okButton = new JButton();
        panel6 = new JPanel();
        panel7 = new JPanel();
        label10 = new JLabel();
        totalsTextField8 = new JTextField();
        label11 = new JLabel();
        realTsTextField = new JTextField();
        label12 = new JLabel();
        timeTextField10 = new JTextField();
        panel8 = new JPanel();
        scrollPane1 = new JScrollPane();
        table1 = new JTable();

        //======== this ========
        setVisible(true);
        setTitle("sunny\u5165\u5e93");
        setClosable(true);
        setIconifiable(true);
        var contentPane = getContentPane();
        contentPane.setLayout(null);

        //======== panel1 ========
        {
            panel1.setPreferredSize(new Dimension(382, 40));
            panel1.setLayout(null);

            //---- enterButton ----
            enterButton.setText("\u5165\u5e93");
            enterButton.addActionListener(e -> enter(e));
            panel1.add(enterButton);
            enterButton.setBounds(new Rectangle(new Point(224, 6), enterButton.getPreferredSize()));

            //---- cancelButton ----
            cancelButton.setText("\u53d6\u6d88");
            cancelButton.addActionListener(e -> cancel(e));
            panel1.add(cancelButton);
            cancelButton.setBounds(new Rectangle(new Point(350, 6), cancelButton.getPreferredSize()));

            {
                // compute preferred size
                Dimension preferredSize = new Dimension();
                for(int i = 0; i < panel1.getComponentCount(); i++) {
                    Rectangle bounds = panel1.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = panel1.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                panel1.setMinimumSize(preferredSize);
                panel1.setPreferredSize(preferredSize);
            }
        }
        contentPane.add(panel1);
        panel1.setBounds(0, 509, 640, panel1.getPreferredSize().height);

        //======== panel2 ========
        {
            panel2.setPreferredSize(new Dimension(0, 150));
            panel2.setBorder(new TitledBorder("\u8bf7\u9009\u62e9\u4f9b\u5e94\u5546"));
            panel2.setLayout(null);

            //---- label1 ----
            label1.setText("\u4f9b\u5e94\u5546\u7f16\u53f7\uff1a");
            panel2.add(label1);
            label1.setBounds(55, 28, label1.getPreferredSize().width, 21);

            //---- supplierComboBox ----
            supplierComboBox.addActionListener(e -> supplier(e));
            panel2.add(supplierComboBox);
            supplierComboBox.setBounds(406, 25, 130, 29);

            //---- supplierIDTextField ----
            supplierIDTextField.setEditable(false);
            panel2.add(supplierIDTextField);
            supplierIDTextField.setBounds(127, 25, 115, supplierIDTextField.getPreferredSize().height);

            //---- label2 ----
            label2.setText("\u4f9b\u5e94\u5546\uff1a");
            panel2.add(label2);
            label2.setBounds(358, 30, label2.getPreferredSize().width, 19);

            //---- label3 ----
            label3.setText("\u8054\u7cfb\u4eba\uff1a");
            panel2.add(label3);
            label3.setBounds(78, 73, label3.getPreferredSize().width, 19);

            //---- manTextField ----
            manTextField.setEditable(false);
            panel2.add(manTextField);
            manTextField.setBounds(128, 71, 114, manTextField.getPreferredSize().height);

            //---- label4 ----
            label4.setText("\u5730\u5740\uff1a");
            panel2.add(label4);
            label4.setBounds(369, 78, label4.getPreferredSize().width, 19);

            //---- addressTextField ----
            addressTextField.setEditable(false);
            panel2.add(addressTextField);
            addressTextField.setBounds(407, 73, 130, addressTextField.getPreferredSize().height);

            //---- label5 ----
            label5.setText("\u8054\u7cfb\u7535\u8bdd\uff1a");
            panel2.add(label5);
            label5.setBounds(65, 114, label5.getPreferredSize().width, 21);

            //---- phoneTextField ----
            phoneTextField.setEditable(false);
            panel2.add(phoneTextField);
            phoneTextField.setBounds(125, 110, 140, phoneTextField.getPreferredSize().height);

            //---- addSupplierButton ----
            addSupplierButton.setText("\u6dfb\u52a0\u4f9b\u5e94\u5546");
            addSupplierButton.addActionListener(e -> addSupplier(e));
            panel2.add(addSupplierButton);
            addSupplierButton.setBounds(366, 109, 94, addSupplierButton.getPreferredSize().height);

            //---- refreshButton ----
            refreshButton.setText("\u5237\u65b0");
            refreshButton.addActionListener(e -> refresh(e));
            panel2.add(refreshButton);
            refreshButton.setBounds(new Rectangle(new Point(523, 109), refreshButton.getPreferredSize()));

            {
                // compute preferred size
                Dimension preferredSize = new Dimension();
                for(int i = 0; i < panel2.getComponentCount(); i++) {
                    Rectangle bounds = panel2.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = panel2.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                panel2.setMinimumSize(preferredSize);
                panel2.setPreferredSize(preferredSize);
            }
        }
        contentPane.add(panel2);
        panel2.setBounds(0, 0, 633, panel2.getPreferredSize().height);

        //======== panel3 ========
        {
            panel3.setLayout(new BorderLayout());

            //======== panel4 ========
            {
                panel4.setPreferredSize(new Dimension(332, 40));
                panel4.setLayout(null);

                //---- addButton ----
                addButton.setText("\u52a0\u5165\u5165\u5e93\u5217\u8868");
                addButton.addActionListener(e -> add(e));
                panel4.add(addButton);
                addButton.setBounds(new Rectangle(new Point(174, 8), addButton.getPreferredSize()));

                //---- yesButton ----
                yesButton.setText("\u786e\u8ba4\u5165\u5e93\u5217\u8868");
                yesButton.addActionListener(e -> yes(e));
                panel4.add(yesButton);
                yesButton.setBounds(new Rectangle(new Point(351, 7), yesButton.getPreferredSize()));

                {
                    // compute preferred size
                    Dimension preferredSize = new Dimension();
                    for(int i = 0; i < panel4.getComponentCount(); i++) {
                        Rectangle bounds = panel4.getComponent(i).getBounds();
                        preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                        preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                    }
                    Insets insets = panel4.getInsets();
                    preferredSize.width += insets.right;
                    preferredSize.height += insets.bottom;
                    panel4.setMinimumSize(preferredSize);
                    panel4.setPreferredSize(preferredSize);
                }
            }
            panel3.add(panel4, BorderLayout.SOUTH);

            //======== panel5 ========
            {
                panel5.setPreferredSize(new Dimension(0, 120));
                panel5.setBorder(new TitledBorder("\u9009\u62e9\u5165\u5e93\u5546\u54c1"));
                panel5.setLayout(null);

                //---- label6 ----
                label6.setText("\u5546\u54c1\uff1a");
                panel5.add(label6);
                label6.setBounds(34, 35, label6.getPreferredSize().width, 20);
                panel5.add(tradeComboBox);
                tradeComboBox.setBounds(72, 32, 139, tradeComboBox.getPreferredSize().height);

                //---- label7 ----
                label7.setText("\u6570\u91cf\uff1a");
                panel5.add(label7);
                label7.setBounds(248, 37, label7.getPreferredSize().width, 19);
                panel5.add(numberTextField);
                numberTextField.setBounds(286, 33, 95, numberTextField.getPreferredSize().height);

                //---- addTradeButton ----
                addTradeButton.setText("\u65b0\u589e\u5546\u54c1");
                addTradeButton.addActionListener(e -> addTrade(e));
                panel5.add(addTradeButton);
                addTradeButton.setBounds(new Rectangle(new Point(470, 77), addTradeButton.getPreferredSize()));

                //---- label8 ----
                label8.setText("\u5355\u4ef7\uff1a");
                panel5.add(label8);
                label8.setBounds(414, 37, label8.getPreferredSize().width, 20);

                //---- priceTextField ----
                priceTextField.setEditable(false);
                panel5.add(priceTextField);
                priceTextField.setBounds(452, 34, 68, priceTextField.getPreferredSize().height);

                //---- label9 ----
                label9.setText("\u603b\u4ef7\uff1a");
                panel5.add(label9);
                label9.setBounds(31, 82, label9.getPreferredSize().width, 20);

                //---- totalTextField ----
                totalTextField.setEditable(false);
                panel5.add(totalTextField);
                totalTextField.setBounds(73, 77, 64, totalTextField.getPreferredSize().height);

                //---- label13 ----
                label13.setText("\u4f18\u60e0\uff1a");
                panel5.add(label13);
                label13.setBounds(182, 82, label13.getPreferredSize().width, 21);

                //---- discountTextField ----
                discountTextField.setEditable(false);
                panel5.add(discountTextField);
                discountTextField.setBounds(221, 78, 60, discountTextField.getPreferredSize().height);

                //---- label14 ----
                label14.setText("\u5b9e\u4ed8\uff1a");
                panel5.add(label14);
                label14.setBounds(317, 79, label14.getPreferredSize().width, 20);

                //---- realsTextField ----
                realsTextField.setEditable(false);
                panel5.add(realsTextField);
                realsTextField.setBounds(357, 77, 60, realsTextField.getPreferredSize().height);

                //---- okButton ----
                okButton.setText("\u67e5\u770b\u4ef7\u683c");
                okButton.addActionListener(e -> ok(e));
                panel5.add(okButton);
                okButton.setBounds(new Rectangle(new Point(546, 34), okButton.getPreferredSize()));

                {
                    // compute preferred size
                    Dimension preferredSize = new Dimension();
                    for(int i = 0; i < panel5.getComponentCount(); i++) {
                        Rectangle bounds = panel5.getComponent(i).getBounds();
                        preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                        preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                    }
                    Insets insets = panel5.getInsets();
                    preferredSize.width += insets.right;
                    preferredSize.height += insets.bottom;
                    panel5.setMinimumSize(preferredSize);
                    panel5.setPreferredSize(preferredSize);
                }
            }
            panel3.add(panel5, BorderLayout.NORTH);

            //======== panel6 ========
            {
                panel6.setLayout(new BorderLayout());

                //======== panel7 ========
                {
                    panel7.setPreferredSize(new Dimension(0, 50));
                    panel7.setLayout(null);

                    //---- label10 ----
                    label10.setText("\u603b\u7801\u6d0b\uff1a");
                    panel7.add(label10);
                    label10.setBounds(57, 15, label10.getPreferredSize().width, 19);

                    //---- totalsTextField8 ----
                    totalsTextField8.setEditable(false);
                    panel7.add(totalsTextField8);
                    totalsTextField8.setBounds(108, 11, 90, totalsTextField8.getPreferredSize().height);

                    //---- label11 ----
                    label11.setText("\u603b\u5b9e\u6d0b\uff1a");
                    panel7.add(label11);
                    label11.setBounds(250, 15, label11.getPreferredSize().width, 19);

                    //---- realTsTextField ----
                    realTsTextField.setEditable(false);
                    panel7.add(realTsTextField);
                    realTsTextField.setBounds(298, 11, 95, realTsTextField.getPreferredSize().height);

                    //---- label12 ----
                    label12.setText("\u65e5\u671f\uff1a");
                    panel7.add(label12);
                    label12.setBounds(435, 13, label12.getPreferredSize().width, 22);

                    //---- timeTextField10 ----
                    timeTextField10.setEditable(false);
                    panel7.add(timeTextField10);
                    timeTextField10.setBounds(475, 10, 105, timeTextField10.getPreferredSize().height);

                    {
                        // compute preferred size
                        Dimension preferredSize = new Dimension();
                        for(int i = 0; i < panel7.getComponentCount(); i++) {
                            Rectangle bounds = panel7.getComponent(i).getBounds();
                            preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                            preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                        }
                        Insets insets = panel7.getInsets();
                        preferredSize.width += insets.right;
                        preferredSize.height += insets.bottom;
                        panel7.setMinimumSize(preferredSize);
                        panel7.setPreferredSize(preferredSize);
                    }
                }
                panel6.add(panel7, BorderLayout.SOUTH);

                //======== panel8 ========
                {
                    panel8.setBorder(new TitledBorder("\u5165\u5e93\u5546\u54c1\u5217\u8868"));
                    panel8.setLayout(null);

                    //======== scrollPane1 ========
                    {
                        scrollPane1.setViewportView(table1);
                    }
                    panel8.add(scrollPane1);
                    scrollPane1.setBounds(5, 18, 620, 127);

                    {
                        // compute preferred size
                        Dimension preferredSize = new Dimension();
                        for(int i = 0; i < panel8.getComponentCount(); i++) {
                            Rectangle bounds = panel8.getComponent(i).getBounds();
                            preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                            preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                        }
                        Insets insets = panel8.getInsets();
                        preferredSize.width += insets.right;
                        preferredSize.height += insets.bottom;
                        panel8.setMinimumSize(preferredSize);
                        panel8.setPreferredSize(preferredSize);
                    }
                }
                panel6.add(panel8, BorderLayout.CENTER);
            }
            panel3.add(panel6, BorderLayout.CENTER);
        }
        contentPane.add(panel3);
        panel3.setBounds(0, 150, 633, 359);

        contentPane.setPreferredSize(new Dimension(640, 580));
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JPanel panel1;
    private JButton enterButton;
    private JButton cancelButton;
    private JPanel panel2;
    private JLabel label1;
    private JComboBox supplierComboBox;
    private JTextField supplierIDTextField;
    private JLabel label2;
    private JLabel label3;
    private JTextField manTextField;
    private JLabel label4;
    private JTextField addressTextField;
    private JLabel label5;
    private JTextField phoneTextField;
    private JButton addSupplierButton;
    private JButton refreshButton;
    private JPanel panel3;
    private JPanel panel4;
    private JButton addButton;
    private JButton yesButton;
    private JPanel panel5;
    private JLabel label6;
    private JComboBox tradeComboBox;
    private JLabel label7;
    private JTextField numberTextField;
    private JButton addTradeButton;
    private JLabel label8;
    private JTextField priceTextField;
    private JLabel label9;
    private JTextField totalTextField;
    private JLabel label13;
    private JTextField discountTextField;
    private JLabel label14;
    private JTextField realsTextField;
    private JButton okButton;
    private JPanel panel6;
    private JPanel panel7;
    private JLabel label10;
    private JTextField totalsTextField8;
    private JLabel label11;
    private JTextField realTsTextField;
    private JLabel label12;
    private JTextField timeTextField10;
    private JPanel panel8;
    private JScrollPane scrollPane1;
    private JTable table1;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
